package pigshare.picture;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

import pigshare.beans.LoginBean;
import pigshare.database.DatabaseManager;


//public class ImageBean {
// private StreamedContent image;
// private Blob bl;
// private String cookieValue;
// private InputStream stream;
//
// public ImageBean() {
// System.out.println("im here");
// HttpServletRequest httpServletRequest = (HttpServletRequest) FacesContext
// .getCurrentInstance().getExternalContext().getRequest();
// Cookie[] cookies = httpServletRequest.getCookies();
//
// if (cookies != null) {
// for (int i = 0; i < cookies.length; i++) {
// if (cookies[i].getName().equalsIgnoreCase("username")) {
// cookieValue = cookies[i].getValue();
// }
// }
// }
// ResultSet rs = DatabaseManager.getDatabaseManager().sendQuery(
// "SELECT pic FROM user WHERE username = " + cookieValue + ";");
// try {
// if (rs.next()) {
// bl = rs.getBlob("pic");
// }
// } catch (SQLException e1) {
// e1.printStackTrace();
// }
//
// try {
// stream = bl.getBinaryStream();
// } catch (SQLException e) {
// e.printStackTrace();
// }
// image = new DefaultStreamedContent(stream, "image/jpeg");
// }
//
// public StreamedContent getImage() {
// System.out.println("getImage");
// return this.image;
// }
@RequestScoped
@ManagedBean(name = "imageBean")
public class ImageBean {
	private DefaultStreamedContent content;
	private String cookieValue;
	private Blob bl;
	private InputStream stream;
	
	private Logger log = Logger.getLogger(LoginBean.class);

	public StreamedContent getContent() {
		if (content == null) {

			HttpServletRequest httpServletRequest = (HttpServletRequest) FacesContext
					.getCurrentInstance().getExternalContext().getRequest();
			Cookie[] cookies = httpServletRequest.getCookies();

			if (cookies != null) {
				for (int i = 0; i < cookies.length; i++) {
					if (cookies[i].getName().equalsIgnoreCase("id")) {
						cookieValue = cookies[i].getValue();
					}
				}
			}
			ResultSet rs = DatabaseManager.getDatabaseManager().sendQuery(
					"SELECT pic FROM user WHERE id = '" + cookieValue + "';");

			try {
				if (rs.next()) {
					bl = rs.getBlob("pic");
					log.info("length of the profilepicture of the user with the id " + cookieValue + " " + bl.length());
					stream = bl.getBinaryStream();
					// /* use your database call here */
					// BufferedInputStream in = new
					// BufferedInputStream(ImageBean.class
					// .getClassLoader().getResourceAsStream("test/test.png"));
					BufferedInputStream in = new BufferedInputStream(stream);
					ByteArrayOutputStream out = new ByteArrayOutputStream();

					int val = -1;
					/*
					 * this is a simple test method to double check values from
					 * the stream
					 */
					try {
						while ((val = in.read()) != -1)
							out.write(val);
					} catch (IOException e) {
						e.printStackTrace();
					}

					byte[] bytes = out.toByteArray();
					content = new DefaultStreamedContent(
							new ByteArrayInputStream(bytes), "image/png",
							"test.png");
				}
			} catch (SQLException e1) {
				e1.printStackTrace();
			}

		}

		return content;
	}

}